package com.wanmait.zaisousuo.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
	public static void addCookie(String name, String value, int days, HttpServletResponse response) {
		Cookie cookie = new Cookie(name,value);
		cookie.setMaxAge(60*60*24*days);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	public static Cookie getCookie(String name, HttpServletRequest request) {
		Cookie[] cookies= request.getCookies();
		if(cookies==null) return null;
		for(Cookie cookie:cookies) {
			if(name.equals(cookie.getName())) {
				return cookie;
			}
		}
		return null;
	}
	public static String getCookieValue(String name, HttpServletRequest request) {
		Cookie cookie= getCookie(name,request);
		if(cookie==null) {
			return null;
		}
		return cookie.getValue();
	}
}










